package com.tj.bo.trade.server.service;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.google.common.collect.Lists;
import com.tj.bo.product.client.ProductService;
import com.tj.bo.product.client.vo.Product;
import com.tj.bo.trade.client.TradeQueryService;
import com.tj.bo.trade.client.enums.OrderStatusEnum;
import com.tj.bo.trade.client.enums.ProfitStatusEnum;
import com.tj.bo.trade.client.request.TradeByPageQueryRO;
import com.tj.bo.trade.client.response.TradeInfoListRO;
import com.tj.bo.trade.client.response.TradeInfoRO;
import com.tj.bo.trade.server.dao.BinaryTradeDao;
import com.tj.bo.trade.server.entity.BinaryTradeEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @program: binary-option
 * @description
 * @author: binary
 * @create: 2020-06-05 14:47
 **/
@Service
public class TradeQueryServiceImpl implements TradeQueryService {

    @Autowired
    private BinaryTradeDao binaryTradeDao;
    @Autowired
    private ProductService productService;

    @Override
    public TradeInfoRO queryTradeInfo(String tradeNo) {
        BinaryTradeEntity binaryTradeEntity = binaryTradeDao.selectBinaryTradeByTradeNo(tradeNo);
        if(binaryTradeEntity != null){
            TradeInfoRO tradeInfoRO = new TradeInfoRO();
            this.packageTradeInfo(binaryTradeEntity, tradeInfoRO);
            return tradeInfoRO;
        }
        return null;
    }

    private void packageTradeInfo(BinaryTradeEntity entity, TradeInfoRO tradeInfoRO){
        BeanUtils.copyProperties(entity, tradeInfoRO);
        Product product = productService.getProductByCode(entity.getProductCode());
        if(entity.getOrderStatus() == OrderStatusEnum.CLOSING.getCode()){
            tradeInfoRO.setRefreshSeconds(0);
            if(entity.getProfitStatus() == ProfitStatusEnum.PROFIT.getCode()){
                tradeInfoRO.setOrderStatusText("Your forecast was correct");
            }else{
                tradeInfoRO.setOrderStatusText("Your forecast was incorrect");
            }
        }else if(entity.getOrderStatus() == OrderStatusEnum.TIMEOUT_CLOSING.getCode()){
            tradeInfoRO.setOrderStatusText("Amount[PKR " +entity.getAmount() + "] returned");
        }else{
            tradeInfoRO.setRefreshSeconds(DateUtil.diffDateToSecond(entity.getExpectDeadline(), new Date()) <= 0 ? 0 : DateUtil.diffDateToSecond(entity.getExpectDeadline(), new Date()));
        }
        tradeInfoRO.setProductName(product.getName());
        tradeInfoRO.setProductIcon(product.getLogo());
        tradeInfoRO.setExpectDeadline(entity.getExpectDeadline().getTime());
        tradeInfoRO.setProfitPercent(new BigDecimal(100).multiply(entity.getSettlementProfit()).intValue() + "%");
    }

    @Override
    public TradeInfoListRO queryTradeInfoListByPage(TradeByPageQueryRO tradeByPageQueryRO) {
        List<BinaryTradeEntity> binaryTradeEntities = binaryTradeDao.selectBinaryTradeListByPageCondition(tradeByPageQueryRO.getPage(), tradeByPageQueryRO.getTradeType(), tradeByPageQueryRO.getUserCode());
        TradeInfoListRO tradeInfoList = new TradeInfoListRO();
        BeanUtils.copyProperties(tradeByPageQueryRO.getPage(), tradeInfoList);
        tradeInfoList.setTradeInfoList(this.convertTradeList(binaryTradeEntities));
        tradeInfoList.setHasNextPage(tradeByPageQueryRO.getPage().getCurrentPage() < tradeByPageQueryRO.getPage().getTotalPageNum());
        return tradeInfoList;
    }

    private List<TradeInfoRO> convertTradeList(List<BinaryTradeEntity> binaryTradeEntities){
        List<TradeInfoRO> tradeInfos = Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(binaryTradeEntities)){
            for(BinaryTradeEntity entity : binaryTradeEntities){
                TradeInfoRO tradeInfoRO = new TradeInfoRO();
                this.packageTradeInfo(entity, tradeInfoRO);
                tradeInfos.add(tradeInfoRO);
            }
        }
        return tradeInfos;
    }

    @Override
    public Integer countOpeningOrdersByUserCode(String userCode, Integer tradeType) {
        return binaryTradeDao.countBinaryTrade(userCode, OrderStatusEnum.OPENING.getCode(), tradeType);
    }

    @Override
    public List<TradeInfoRO> queryOpeningOrdersByUserCode(String userCode, Integer tradeType, String productCode) {
        List<BinaryTradeEntity> binaryTradeEntities = binaryTradeDao.selectBinaryTradeListByUserCodeAndProductCode(userCode, tradeType, OrderStatusEnum.OPENING.getCode(), productCode);
        List<TradeInfoRO> tradeInfos = Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(binaryTradeEntities)){
            binaryTradeEntities.stream().forEach(s->{
                TradeInfoRO tradeInfo = new TradeInfoRO();
                BeanUtils.copyProperties(s, tradeInfo);
                tradeInfos.add(tradeInfo);
            });
        }
        return tradeInfos;
    }

    @Override
    public boolean isProductNewUser(String userCode, String productCode) {
        return binaryTradeDao.isProductNewUser(userCode, productCode);
    }

    @Override
    public List<TradeInfoRO> getOpeningOrders() {
        List<BinaryTradeEntity> binaryTradeEntities = binaryTradeDao.selectsBinaryTradeListByCondition(OrderStatusEnum.OPENING.getCode());
        List<TradeInfoRO> tradeInfos = Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(binaryTradeEntities)){
            binaryTradeEntities.stream().forEach(s->{
                TradeInfoRO tradeInfo = new TradeInfoRO();
                BeanUtils.copyProperties(s, tradeInfo);
                tradeInfos.add(tradeInfo);
            });
        }
        return tradeInfos;
    }
}
